package com.miyake.client.profile;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.client.DoctorsUserService;
import com.miyake.client.DoctorsUserServiceAsync;
import com.miyake.client.tools.ClickableImage;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.ImageListElement;
import com.miyake.client.tools.SmallButton;

public abstract class FriendList2 extends Composite {
	private DoctorsUserServiceAsync service = DoctorsUserService.Util.getInstance();

	abstract void onUserChange(String key);
	abstract void onFind();
	
	public FriendList2(String key, boolean me) {
		VerticalPanel verticalPanel = new VerticalPanel();
//		verticalPanel.setBorderWidth(1);
		initWidget(verticalPanel);
		verticalPanel.add(new HTML("<h2>My Friends</h2>"));
		
		// Me can find friends.
		HorizontalPanel bar = new HorizontalPanel();
		verticalPanel.add(bar);
		if (me) {
			bar.add(new SmallButton("Find Friends", new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					onFind();
				}
			}));
		}
		getUserList(key, verticalPanel);
	}

	private void getUserList(String key, final VerticalPanel verticalPanel) {
		service.getFriendList(key, new AsyncCallback<List<ImageListElement>>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<ImageListElement> result) {
				showList(result, verticalPanel);
			}
		});
	}

	protected void showList(List<ImageListElement> result,
			VerticalPanel verticalPanel) {
		final Iterator<ImageListElement> it = result.iterator();
		ThumbNail thumbNail = new ThumbNail(result.size(), 6) {
			@Override
			protected Widget getWidget(final int i) {
				return createElement(i, it.next());
			}
		};
		verticalPanel.add(thumbNail);
		thumbNail.show();
	}

	protected Widget createElement(int i, final ImageListElement next) {		
		VerticalPanel v = new VerticalPanel();
		Image image = new ClickableImage();
		image.setSize("70px", "70px");
		if (next.getImageURL() != null) {
			image.setUrl(next.getImageURL());
		}
		else {
			image.setUrl("FaceSample.PNG");
		}
		image.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {

			}
		});
		v.add(image);
		
		ClickHandler clickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				onUserChange(next.getKey());
			}
		};
		String name = next.getName();
//		System.out.println(name);
		if (name == null) {
			name = "No Name";
		}
		v.add(new ClickableLabel(name, clickHandler));
		image.addClickHandler(clickHandler);

		return v;
	}

	
}
